import { redirect, replace } from "react-router";
// import { commitSession, getSession } from "~/session.server";
import type { Route } from "./+types/logout";
import { serialize, parse } from "cookie";

export const action = async (params: Route.ActionArgs) => {
  const { request } = params
  const cookies = parse(request.headers.get("Cookie") ?? '');
  const auth = cookies.auth
  console.log('when login, auth is', auth);
  const newAuth = '123456'
  console.log('after login, auth is', newAuth);

  const cookie = serialize('auth', newAuth, {
    maxAge: 60,
    path: '/',
    httpOnly: true,
    sameSite: 'lax',
  })

  console.log('cookie is', cookie);
  const { promise, resolve } = Promise.withResolvers()
  setTimeout(() => {
    resolve(replace('/session-test', {
      headers: { 'Set-Cookie': cookie }
    }))
  }, 1000)

  return promise
}
/* export const action = async (params: Route.ActionArgs) => {
  const { request } = params
  const session = await getSession(request.headers.get("Cookie"));
  const auth = session.get('auth')
  console.log('when login, auth is', auth);
  session.set('auth', '123456');
  const newAuth = session.get('auth')
  console.log('after login, auth is', newAuth);

  const cookie = await commitSession(session)
  console.log('cookie is', cookie);

  return replace('/session-test', {
    headers: { 'Set-Cookie': await commitSession(session) }
  })
} */